<?php
namespace app\controller;

use app\BaseController;
use \think\facade\Request;
use \think\facade\Cache;
use \think\facade\Db;
use \think\facade\Log;
use think\facade\Filesystem;
class UplodeFile extends BaseController
{
  // h5把图片上传到本地
  public function uplodeImageLocalH5(Request $request)
  {
	  $params = Request::param();
	  $file = request()->file("file");
	  //验证图片格式
	  $originalExtension = $file->getOriginalExtension();
	  // $arr = ['jpg','png'];
	  // if(!in_array($originalExtension,$arr)){
	  // 		return $this->returnJson(400,'文件格式错误',[]);
	  // }
	  //文件名称
	  $img_name = $file->getOriginalName();
	  // 把上传的excel文件下载到本地一份
	  $savename = \think\facade\Filesystem::disk('public')->putFile('file', $file);
	  $img_url = str_replace("\\","/",$savename);
	  $img_url =  'http://www.hs.com/storage/'.$img_url;
	  
	  $dd = [
	  		 'img_url'=>$img_url,
	  		 'add_time'=>date("Y-m-d H:i:s"),
	  		 'cid'=>$params['cid'],
	  		 'img_name'=>$img_name,
	  		 'savename'=>$savename,
			 'code'=>$params['code'],
	  ];
	  Db::table('code_img')->insert($dd);
	  return $this->returnJson(200,'操作成功',['img_url'=>$img_url]);
  }
  //pc把图片上传到本地
  public function uplodeImageLocal(Request $request)
  {
     $params = Request::param();
     $file = request()->file("file");
	 //验证图片格式
	 $originalExtension = $file->getOriginalExtension();
	 // $arr = ['jpg','png'];
	 // if(!in_array($originalExtension,$arr)){
		// return $this->returnJson(400,'文件格式错误',[]);
	 // }
	 
	 //文件名称
	 $img_name = $file->getOriginalName();
	
	 // 把上传的excel文件下载到本地一份
	 $savename = \think\facade\Filesystem::disk('public')->putFile('file', $file);
	 $img_url = str_replace("\\","/",$savename);
	 $img_url =  'http://nccode.huazhixi.com/storage/'.$img_url;
	 
	 $dd = [
		 'img_url'=>$img_url,
		 'add_time'=>date("Y-m-d H:i:s"),
		 'img_name'=>$img_name,
		 'savename'=>$savename,
		 'cid'=>$params['cid']
	 ];
     Db::table('img_sys')->insert($dd);
	 return $this->returnJson(200,'操作成功',['img_url'=>$img_url]);
	
  }
  //pc把图片上传到腾讯云
  public function uplodeImage(Request $request)
  {
     $params = Request::param();
     $file = request()->file("file");
  	 
  	 //验证图片格式
  	 $originalExtension = $file->getOriginalExtension();
  	 $arr = ['jpg','png'];
  	 if(!in_array($originalExtension,$arr)){
  		return $this->returnJson(400,'文件格式错误',[]);
  	 }
	 
  	 $res = \TencentUpload::upload($file->getPathname(),$file->getMime(),$file);
	  
	  if($res['code']==200){
	       $dd = [
		 'img_url'=>$res['data'],
		 'add_time'=>date("Y-m-d H:i:s"),
		 'cid'=>$params['cid'],
		 'img_name'=>$res['savename'],
		 'savename'=>$res['savename']
	    ];
	    
        Db::table('hs_img_sys')->insert($dd);
	    return $this->returnJson(200,'操作成功',['img_url'=>$res['data']]);
	  }else{
	       return $this->returnJson(400,'操作失败',[]);
	  }
  	
  }
  // h5把图片上传到腾讯云
  public function uplodeImageH5(Request $request)
  {
  	  $params = Request::param();
  	  $file = request()->file("file");
  	  
  	  //验证图片格式
  	  $originalExtension = $file->getOriginalExtension();
  	  $arr = ['jpg','png'];
  	  if(!in_array($originalExtension,$arr)){
  	      return $this->returnJson(400,'文件格式错误',[]);
  	  }
  	  
  	  $res = \TencentUpload::upload($file->getPathname(),$file->getMime(),$file);
  	  
  	   if($res['code']==200){
  		   $dd = [
  		   		 'img_url'=>$res['data'],
  		   		 'add_time'=>date("Y-m-d H:i:s"),
  		   		 'cid'=>$params['cid'],
  		   		 'img_name'=>$res['savename'],
  		   		 'savename'=>$res['savename'],
  		   ];
  		   Db::table('hs_img_sys')->insert($dd);
  		   return $this->returnJson(200,'操作成功',['img_url'=>$res['data']]);
  	   }else{
  		   return $this->returnJson(400,'操作失败',[]); 
  	   }
  	   
  }
  
}